package com.szf.system.service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import com.szf.base.service.BaseService;
import com.szf.system.entity.ComParam;
import com.szf.system.entity.ComParamType;
import com.szf.util.MapUtil;
import com.szf.util.StringUtil;

@Service
public class ParamService extends BaseService {

	private Map<String, LinkedHashMap<String, String>> comParam;

	@PostConstruct
	public void init() {
		Map<String, LinkedHashMap<String, String>> tmp = new HashMap<String, LinkedHashMap<String, String>>();
		List<Map> list = super.ibatisDao.queryForList("sinlog.queryComParam");
		for (Map map : list) {
			String typeCode = MapUtils.getString(map, "TYPE_CODE");
			String value = MapUtils.getString(map, "PARAM_VALUE");
			String valueCn = MapUtils.getString(map, "PARAM_VALUE_CN");
			LinkedHashMap<String, String> subMap = tmp.get(typeCode);
			if (subMap == null) {
				subMap = new LinkedHashMap<String, String>();
				tmp.put(typeCode, subMap);
			}
			if (!StringUtil.isEmpty(value) && !StringUtil.isEmpty(valueCn)) {
				subMap.put(value, valueCn);
			}
		}
		comParam = tmp;
	}

	public ComParamType findParamTypeById(String typeId) {
		return super.hibernateDao.get(ComParamType.class, typeId);
	}

	public ComParam findParamById(String paramId) {
		return super.hibernateDao.get(ComParam.class, paramId);
	}

	public ComParamType findParamTypeByTypeCode(String typeCode) {
		List<ComParamType> list = super.hibernateDao.findBy(ComParamType.class, "typeCode", typeCode);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	public ComParam findParamByParamValue(String value, String typeId) {
		String hql = "from ComParam p where p.paramValue=:paramValue and p.typeId=:typeId";
		Map param = MapUtil.param("paramValue", value, "typeId", typeId);
		List<ComParam> list = super.hibernateDao.query(hql, param);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;

	}

	public void saveParamType(ComParamType paramType) {
		super.hibernateDao.saveOrUpdate(paramType);
	}

	public void saveParam(ComParam param) {
		super.hibernateDao.saveOrUpdate(param);
	}

	public void deleteParamType(String ids) {
		if (StringUtil.isEmpty(ids)) {
			return;
		}
		String inIds = StringUtil.getInString(ids);
		Map<String, String> param = new HashMap<String, String>();
		param.put("ids", inIds);
		super.ibatisDao.delete("sinlog.deleteParamType", param);
	}

	public void deleteParam(String ids) {
		if (StringUtil.isEmpty(ids)) {
			return;
		}
		String inIds = StringUtil.getInString(ids);
		Map<String, String> param = new HashMap<String, String>();
		param.put("ids", inIds);
		super.ibatisDao.delete("sinlog.deleteParam", param);
	}

	public Map<String, String> getComMap(String typeCode) {
		Map<String, String> tmp = comParam.get(typeCode);
		if (tmp == null) {
			tmp = new HashMap<String, String>();
		}
		return tmp;
	}
}
